package top.soulblack.purchasesale.controller.setting;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.mapper.UserMapper;
import top.soulblack.purchasesale.service.RoleService;

import java.security.Permission;
import java.util.Collection;

import static top.soulblack.purchasesale.common.CodeMsg.*;

@Controller
@RequestMapping("/role")
public class RoleController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RoleService roleService;
    @Autowired
    private UserMapper userMapper;


    @RequestMapping("role")
    @RequiresPermissions("session:role")
    public String online() {
        return "/setting/role_management";
    }

    @RequiresPermissions("role:saveRole")
    @RequestMapping("saveRole")
    @ResponseBody
    public Result<Boolean> saveRole(String name,String oldname) {
        // 判断是否存在当前角色
//        System.out.println(name);
        //System.out.println(roleService.existRole(role));
        if (roleService.existRole(name)) {
            return Result.error(ROlE_EXIST);
        }
        if(oldname==""){
            roleService.addRole(name);
            return Result.success(true);
        }
        else{
            roleService.updateRole(name,oldname);
            return Result.success(true);
        }
    }

    @RequiresPermissions("role:copyasNew")
    @RequestMapping("copyasNew")
    @ResponseBody
    // 后期需要加上User权限验证，否则暴露接口有危险。
    public Result<Boolean> copyasNew(String rolename) {
        rolename=rolename+"(复制)";
        if (roleService.copyasNew(rolename)) {
            return Result.success(true);
        }
        return Result.error(RolE_DELETE);
    }

    @RequestMapping("/deleteRole")
    @ResponseBody
    // 后期需要加上User权限验证，否则暴露接口有危险。
    public Result<Boolean> deletRole(int roleId) {
        if (roleService.deleteRole(roleId)) {
            return Result.success(true);
        }
        return Result.error(RolE_DELETE);
    }

}